package de.keychain

import grails.test.mixin.*
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(LoginService)
@Mock([Configuration,Password, ServiceModel])
class LoginServiceSpec extends Specification {

	Configuration configuration
	def passwordString = "password123"
	
    def setup() {
		
		def systemPassword = new Password(password:passwordString)
		systemPassword.encodePassword()
		
		assert(systemPassword.save())
		
		def systemService = new ServiceModel(name:"KeyCHain",username:"admin")
		
		assert(systemService.save())
		
		//since this is just a unit test
		systemService.passwords = [systemPassword]
		
		configuration = new Configuration(email:"unit-test@mail.de", activationCode:"123", encryptionSalt:"1234")
		configuration.service = systemService
		
		assert(configuration.save())
    }


    void "test if system password is hashed and login works"() {
		given: "valid system login information" 
		
			def username = "admin"
			def password = passwordString
			
		when: "user tries to login"		
		
			def isLoginValid = service.login(username, password)
			
		then: "login result should be true"
			assert(isLoginValid)
    }
}
